package com.hnevc.lirihui;




import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText zh_et;
    private EditText mm_et;
    private Button qd_bt;
    private TextView zc_tv;
    private static final String TAG = "MainActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        findViewById(R.id.zc_tv).setOnClickListener(this);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        zh_et = findViewById(R.id.zh_et);
        mm_et = findViewById(R.id.mm_et);
        qd_bt = findViewById(R.id.qd_bt);
        zc_tv = findViewById(R.id.zc_tv);
    }
    private void initData() {
        String url ="http://10.1.62.39/api/user/login";
        String shoujiStr = zh_et.getText().toString().trim();
        String pawwwordStr = mm_et.getText().toString().trim();
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("telephone",shoujiStr);
            jsonObject.put("password",pawwwordStr);
        }catch (JSONException e){
            e.printStackTrace();
        }
        RequestQueue requestQueue =Volley.newRequestQueue(LoginActivity.this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e(TAG, "onResponse" + jsonObject.optString("msg"));
                Toast.makeText(LoginActivity.this,jsonObject.optString("msg"),Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(LoginActivity.this,"网络请求失败",Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    private void initEvent() {



    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(LoginActivity.this,RegistActivity.class);
        startActivity(intent);
    }

}

